﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;

namespace HY.WEB.HR
{
    public partial class mem_edit : PageBase
    {
        public string  whe = "", path = "";
        public string us, um, ubh,sid;
        string tab = "sys_user"; 
        protected void Page_Load(object sender, EventArgs e)
        {
            
            us = Session["ID"].ToString();
            um = Session["user"].ToString();
            ubh = Session["bh"].ToString();
            sid = Session["sid"].ToString();

            if (!IsPostBack)
            {
                if (!String.IsNullOrEmpty(Request["sid"]))
                {
                     hidsid.Text = Request["sid"].Trim();                
                }else
                {
                    hidsid.Text = sid;

                    //txtlgid.Enabled=true;
                    //txtnm.Enabled = true;
                    //txtlgid.Readonly = true;
                    //txtnm.Readonly = true;

                    ddlstatus.Enabled = false;

                    ddlinout.Enabled = false;
                    ddldetp.Enabled = false;
                    ddlrole.Enabled = false;
                    ddljob.Enabled = false;


                }

                fill();
                fill2();
            }

        
        }

        //填 充ddl
        void fill()
        {
            BLL.HR.hr_ot u = new BLL.HR.hr_ot();
            DataTable e;

            //填充部门
            e = u.getlist_dept(whe);
            ddldetp.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddldetp.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddldetp.DataBind();

            //填充职位
            e = u.getlist_job(whe);
            ddljob.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddljob.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddljob.DataBind();


            //填充角色
            e = u.getlist_role(whe);
            ddlrole.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlrole.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddlrole.DataBind();

            //填充学历
            e = u.getlist_edu(whe);
            ddledu.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddledu.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddledu.DataBind();


        }

        void fill2()
        {
            string dts; DateTime lastday;

            BLL.HR.sys_user S = new BLL.HR.sys_user();
          DataTable d=  S.GetList("sid="+hidsid.Text).Tables[0];


          hidsbh.Text = d.Rows[0]["sbh"].ToString().Trim();
          txtnm.Text = d.Rows[0]["user_name"].ToString().Trim();

           
      
          ddlsex.SelectedIndex = ddlsex.Items.IndexOf(ddlsex.Items.FindByText(d.Rows[0]["sex"].ToString()));

          txtschool.Text = d.Rows[0]["school"].ToString().Trim();

          ddledu.SelectedIndex = ddledu.Items.IndexOf(ddledu.Items.FindByText(d.Rows[0]["edu"].ToString()));

          major.Text = d.Rows[0]["major"].ToString().Trim();

          ddldetp.SelectedIndex = ddldetp.Items.IndexOf(ddldetp.Items.FindByText(d.Rows[0]["dept"].ToString()));

          ddlrole.SelectedIndex = ddlrole.Items.IndexOf(ddlrole.Items.FindByText(d.Rows[0]["role"].ToString()));

          ddljob.SelectedIndex = ddljob.Items.IndexOf(ddljob.Items.FindByText(d.Rows[0]["job"].ToString()));


          txtjg.Text = d.Rows[0]["jg"].ToString().Trim();
          txtaddress.Text = d.Rows[0]["hj_address"].ToString().Trim();
          txtnewadd.Text = d.Rows[0]["cur_address"].ToString().Trim();
          txtcode.Text = d.Rows[0]["code"].ToString().Trim();
          txttel.Text = d.Rows[0]["tel"].ToString().Trim();
          txtjustperson.Text = d.Rows[0]["r_name"].ToString().Trim();
          txtjusttel.Text = d.Rows[0]["r_tel"].ToString().Trim();
          txtqq.Text = d.Rows[0]["qq"].ToString().Trim();
          txtemail.Text = d.Rows[0]["email"].ToString().Trim();

          dts = d.Rows[0]["indate"].ToString().Trim();
          if (dts.Substring(0, 4) != "1900")
          {
               lastday = DateTime.Parse(dts);
              indate.Text = lastday.ToString("yyyy-M-d");
          }

          dts = d.Rows[0]["outdate"].ToString().Trim();
          if (dts.Substring(0, 4) != "1900")
          {
               lastday = DateTime.Parse(dts);
              outdate.Text = lastday.ToString("yyyy-M-d");
          }
         
          txtlgid.Text = d.Rows[0]["lgid"].ToString().Trim();
          
          //txtlgpw.Text = d.Rows[0]["lgpw"].ToString().Trim();
          hidpassword.Text = d.Rows[0]["lgpw"].ToString().Trim();

          ddlstatus.SelectedIndex = ddlstatus.Items.IndexOf(ddlstatus.Items.FindByText(d.Rows[0]["status"].ToString()));

          ddlinout.SelectedIndex = ddlinout.Items.IndexOf(ddlinout.Items.FindByText(d.Rows[0]["inout"].ToString()));
          
          Image1.ImageUrl = d.Rows[0]["pic"].ToString();
         
        }
  
        protected void btnSubmit_Click(object sender, EventArgs e)
        {  
            //文件上传
          
            string fileName = fileup.ShortFileName;
             if (fileName.Length >0)
                    {
                        fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                        fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;
                        path = "~/file/hr/" + fileName;
                        if (fileup.HasFile)
                            fileup.SaveAs(Server.MapPath(path));
                    }
             up_member();


           //string title = txttitel.Text;
            //string person =us;
           // string day = txtday.Text;
          //  string memo = HttpUtility.HtmlEncode(FCKeditor1.Value);
          //  string memo = HttpUtility.HtmlEncode(FCKeditor1.Value);
        
         

           // BLL.oa.ad.insert(title, person, day, memo, path, hidstype.Text);


            Alert.Show("添加完成 ");
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        void up_member()
        {
            Model.HR.sys_user s = new Model.HR.sys_user();
            s.sid =int.Parse(hidsid.Text);
            s.sbh = hidsbh.Text;

            if ( path.Trim()=="")
            {
                path = Image1.ImageUrl;
            }


            s.lgid = txtlgid.Text;

            if (txtlgid.Text == null || txtlgid.Text == "")
            {
                Alert.Show("账号不能为空!");
                return;
            }

            if (BLL.Bllbase.check_exits("sys_user", "  sid<>" + s.sid + " and lgid='" + txtlgid.Text + "'"))
            {
                Alert.Show("账号已存在，请输入其它账号名！");
                return;
            }


            if (txtlgpw.Text != "")
                s.lgpw = BLL.sys.chkpw.Md5(txtlgpw.Text);
            else
                s.lgpw = hidpassword.Text;


            s.user_name = txtnm.Text;
            s.sex = ddlsex.SelectedValue;

            if (VAN.IsDateTime(indate.Text))
                s.indate = DateTime.Parse(indate.Text);
            else
                s.indate = DateTime.Parse("1900-01-01");

            if (VAN.IsDateTime(birthday.Text))
                s.birthday = DateTime.Parse(birthday.Text);
            else
                s.birthday = DateTime.Parse("1900-01-01");

            if (VAN.IsDateTime(outdate.Text))
                s.outdate = DateTime.Parse(outdate.Text);
            else
                s.outdate =DateTime.Parse( "1900-01-01");

       
            s.school=txtschool.Text;

            
            s.edu  =ddledu.SelectedValue;

            
            s.major = major.Text;

            
            s.dept = ddldetp.SelectedValue;

            
            s.role = ddlrole.SelectedValue;

           
            s.job = ddljob.SelectedValue;

        
            s.jg = txtjg.Text;

           
            s.hj_address = txtaddress.Text;

           
            s.cur_address = txtnewadd.Text;

           
            s.code = txtcode.Text;

           
            s.tel = txttel.Text;

           
            s.r_name = txtjustperson.Text;

           
            s.r_tel = txtjusttel.Text;

          
            s.QQ = txtqq.Text;

            
            s.email = txtemail.Text;

           
            s.marry = ddlmarry.SelectedValue; //婚否
            s.pic = path;

            s.status = 0; //开通
            s.status =int.Parse( ddlstatus.SelectedValue);
            s.inout =int.Parse(ddlinout.SelectedValue);  //在职

            BLL.HR.sys_user t = new BLL.HR.sys_user();
            t.Update(s);       
        
        }
       
    }
}